package org.light.shiroauth.verb;

import java.util.ArrayList;
import java.util.List;

import org.light.core.Verb;
import org.light.core.Writeable;
import org.light.domain.Domain;
import org.light.domain.JavascriptBlock;
import org.light.domain.JavascriptMethod;
import org.light.domain.Method;
import org.light.domain.Statement;
import org.light.domain.Type;
import org.light.easyui.EasyUIPositions;
import org.light.exception.ValidateException;
import org.light.utils.StringUtil;
import org.light.utils.WriteableUtil;

public class LogoutUser extends Verb implements EasyUIPositions {

	@Override
	public Method generateDaoImplMethod() throws Exception {
		return null;
	}	

	@Override
	public Method generateDaoMethodDefinition() throws Exception {
		return null;		
	}

	@Override
	public Method generateServiceMethodDefinition() throws Exception {
		return null;
	}

	@Override
	public Method generateServiceImplMethod() throws Exception  {
		return null;
	}

	public LogoutUser(){
		super();
		this.setLabel("注销");
	}
	
	public LogoutUser(Domain domain) throws ValidateException{
		super();
		this.domain = domain;
		this.denied = domain.isVerbDenied("LogoutUser");
		this.setVerbName("Logout"+StringUtil.capFirst(this.domain.getStandardName()));
		this.setLabel("注销");
		if  (domain.getLanguage().equalsIgnoreCase("english"))  this.setLabel("LogoutUser");
	}

	@Override
	public Method generateControllerMethod() throws Exception {
		if (this.denied) return null;
		else {
			Method method = new Method();
			method.setStandardName("logout"+this.domain.getCapFirstDomainName());
			method.setReturnType(new Type("Map<String,Object>"));
			method.setThrowException(true);
			if ("boot3".equalsIgnoreCase(this.domain.getTechnicalStack())||"jpa".equalsIgnoreCase(this.domain.getTechnicalStack())) {
				method.addAdditionalImport("jakarta.servlet.http.HttpServletRequest");
			} else {
				method.addAdditionalImport("javax.servlet.http.HttpServletRequest");
			}
			method.addAdditionalImport("org.apache.shiro.SecurityUtils");
			method.addAdditionalImport(this.domain.getPackageToken()+ "."+this.domain.getDomainSuffix()+"."+this.domain.getCapFirstDomainNameWithSuffix());
			method.addAdditionalImport(this.domain.getPackageToken()+"."+this.domain.getServiceSuffix()+"."+this.domain.getStandardName()+"Service");
			//method.addSignature(new Signature(1,"request","HttpServletRequest"));
			method.addMetaData("RequestMapping(value = \"/logout"+this.domain.getCapFirstDomainName()+"\", method = RequestMethod.GET)");
	
			List<Writeable> sList = new ArrayList<Writeable>();
			sList.add(new Statement(1000L,2,"Map<String,Object> result = new TreeMap<String,Object>();"));
			sList.add(new Statement(2000L,2,"try {"));
			sList.add(new Statement(3000L,3,""+this.domain.getCapFirstDomainNameWithSuffix()+" "+this.domain.getLowerFirstDomainName()+" = ("+this.domain.getCapFirstDomainNameWithSuffix()+")SecurityUtils.getSubject().getPrincipal();"));
			sList.add(new Statement(5000L,3,"SecurityUtils.getSubject().logout();"));
			sList.add(new Statement(6000L,3,""));
			sList.add(new Statement(7000L,2,"} catch (Exception e) {"));
			sList.add(new Statement(8000L,3,"System.err.println(e.getMessage());"));
			sList.add(new Statement(9000L,2,"}"));
			sList.add(new Statement(10000L,2,"result.put(\"success\",true);"));
			sList.add(new Statement(11000L,2,"result.put(\"data\",null);"));
			sList.add(new Statement(12000L,2,"return result;"));
			method.setMethodStatementList(WriteableUtil.merge(sList));
			return method;
		}
	}	

	@Override
	public JavascriptBlock generateEasyUIJSButtonBlock() throws Exception {
		return null;	
	}

	@Override
	public JavascriptMethod generateEasyUIJSActionMethod() throws Exception {
		return null;		
	}

	@Override
	public Method generateDummyDaoImplMethod() throws Exception {
		return null;
	}
}
